package com.stx.xhb.customwaterview;


import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.jar.Attributes;

/**
 * @author: xiaohaibin.
 * @time: 2018/1/5
 * @mail:xhb_199409@163.com
 * @github:https://github.com/xiaohaibin
 * @describe: 自定义仿支付宝蚂蚁森林水滴View
 */

public class WaterView extends Component implements Component.DrawTask {

    private Paint paint;
    private AnimatorValue mAnimator;
    AnimatorProperty animator;
    /**
     * 文字颜色
     */
    private int textColor = Color.getIntColor("#69c78e");
    /**
     * 水滴填充颜色
     */
    private int waterColor = Color.getIntColor("#c3f593");
    /**
     * 球描边颜色
     */
    private int storkeColor = Color.getIntColor("#69c78e");
    /**
     * 描边线条宽度
     */
    private float strokeWidth = 0.5f;
    /**
     * 文字字体大小
     */
    private float textSize = 36;
    /**
     * 水滴球半径
     */
    private int mRadius = 30;
    /**
     * 圆球文字内容
     */
    private String textContent="";

    public WaterView(Context context, String textContent) {
        super(context);
        this.textContent=textContent;
        init();
    }

    public WaterView(Context context,  AttrSet attrs) {
        super(context, attrs);
        init();
    }

    public WaterView(Context context,  AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        paint = new Paint();
        paint.setAntiAlias(true);

        addDrawTask(this);
    }

//    @Override
//    public void draw(Canvas canvas) {
//        super.draw(canvas);
//
//    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        setMeasuredDimension(,);
//    }

//    @Override
//    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//        super.onSizeChanged(w, h, oldw, oldh);
//        Log.i("====》WaterView X",getX()+"==");
//        Log.i("====》WaterView Y",getY()+"==");
//    }

//    @Override
//    protected void onAttachedToWindow() {
//        super.onAttachedToWindow();
//        start();
//    }

//    @Override
//    protected void onDetachedFromWindow() {
//        super.onDetachedFromWindow();
//        stop();
//    }

//    @Override
//    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
//        super.onVisibilityChanged(changedView, visibility);
//        if (visibility == VISIBLE) {
//            start();
//        } else {
//            stop();
//        }
//    }

    private void drawCircleView(Canvas canvas){
        //圆球
        paint.setColor(new Color(waterColor));
        paint.setStyle(Paint.Style.FILL_STYLE);
        canvas.drawCircle(Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), mRadius), paint);

        //描边
        paint.setColor(new Color(storkeColor));
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(Utils.dp2px(getContext(), (int) strokeWidth));
        canvas.drawCircle(Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), (int) (mRadius+strokeWidth)) , paint);

        //圆球文字
        paint.setTextSize((int) textSize);
        paint.setColor(new Color(textColor));
        paint.setStyle(Paint.Style.FILL_STYLE);
        drawVerticalText(canvas, Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), mRadius), textContent);
    }

    private void drawVerticalText(Canvas canvas, float centerX, float centerY, String text) {
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float baseLine = -(fontMetrics.ascent + fontMetrics.descent) / 2;
        float textWidth = paint.measureText(text);
        float startX = centerX - textWidth / 2;
        float endY = centerY + baseLine;
        canvas.drawText(paint,text, startX, endY);
    }


    public void start() {
        if (mAnimator == null) {
            mAnimator = new AnimatorValue();
            animator = createAnimatorProperty();
            mAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                @Override
                public void onUpdate(AnimatorValue animatorValue, float v) {
                    float valueY;
                    if(v <= 0.5f){
                        valueY = -6.0f + 24.0f * v;
                    }else {
                        valueY = -6.0f - (v-1) * 24.0f ;
                    }
                    animator.moveFromY(-6.0f).moveToY(6.0f);
                    animator.moveFromY(6.0f).moveToY(-6.0f);


                }
            });
          //  mAnimator.ofFloat(this, "translationY", -6.0f, 6.0f, -6.0f);
            mAnimator.setDuration(3500);
            mAnimator.setCurveType(Animator.CurveType.LINEAR);
            mAnimator.setLoopedCount(Integer.MAX_VALUE);
//            mAnimator.setRepeatCount(ValueAnimator.INFINITE);
            mAnimator.start();
        } else if (!mAnimator.isRunning()) {
            mAnimator.start();
        }
    }

    public void stop() {
        if (mAnimator != null) {
            mAnimator.cancel();
            mAnimator = null;
        }
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        setWidth(Utils.dp2px(getContext(), (int) (2 * (mRadius+strokeWidth))));
        setHeight(Utils.dp2px(getContext(), (int) (2 * (mRadius+strokeWidth))));
        drawCircleView(canvas);
    }
}
